from bs4 import BeautifulSoup
import requests
import pymysql

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
def getImg(films):
    for f in films:
        response = requests.get(f.imgsrc, headers=headers, verify=False)
        with open(f'doubanImg/{f.rank}_{f.name}.webp', 'wb+') as file:
            file.write(response.content)
def saveFilmsToDB(film):
    conn = pymysql.connect(host='localhost',
                           user='root',
                           password='root',
                           database='pachongdb',
                           cursorclass=pymysql.cursors.DictCursor)
    with conn:
        with conn.cursor() as cursor:
            #sql = "INSERT INTO douban250(filmrank,name,star,quote,score,pjNum,imgsrc) values(%s,%s,%s,%s,%s,%s,%s)"
            sql2=f"INSERT INTO douban250(filmrank,name,star,quote,score,pjNum,imgsrc) values('{film.rank}', '{film.name}','{film.star}','{film.quote}','{film.score}','{film.pjNum}','{film.imgsrc}')"
            try:
                #cursor.execute(sql,(film.rank, film.name,film.star,film.quote,film.score,film.pjNum,film.imgsrc))
                cursor.execute(sql2)
                conn.commit()
                print(f'影片{film.rank}添加成功')
            except:
                conn.rollback()  # 发生错误时回滚
                print('添加失败')
class film():
    def __init__(self, rank, name, star, quote, score,pjNum,imgsrc):
        self.rank = rank
        self.name = name
        self.star = star
        self.score = score
        self.imgsrc = imgsrc
        self.quote = quote
        self.pjNum = pjNum
    def show(self):
        print(f'第{self.rank}名', self.name, self.star, self.quote, self.score, self.pjNum,self.imgsrc,)
text = ''
with open('douban250.html', 'r', encoding='utf-8') as f:
    text = f.read()
soup = BeautifulSoup(text, 'lxml')
items = soup.find_all("div", class_="item")
films = []
for i in items:
    rank = i.select("em")[0].get_text()  # 排名
    img = i.select("img")[0]
    imgsrc = img["src"]  # 照片url
    name = i.select("div[class='hd'] span")[0].get_text()  # 电影名
    star = i.select("div[class='bd'] p")[0].get_text().replace("\n", "").strip()  # 导演、主演
    score = i.select("span[class='rating_num']")[0].get_text()  # 得分
    quote = i.select("span[class='inq']")[0].get_text()  # 评语
    pjNum = i.select("div[class='star'] span")[3].get_text()  # 评价人数
    fi = film(rank, name, star, quote, score, pjNum,imgsrc)
    # fi.show()
    # saveFilmsToDB(fi)
    films.append(fi)
getImg(films)


